package me.firstapp.api.system;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.system.ApiClient;
import me.firstapp.module.system.ApiClientRecord;
import me.firstapp.service.system.ApiClientRecordService;
import me.firstapp.service.system.ApiClientService;

@Controller
@RequestMapping(value = "/security")
@Api(value = "/security", description = "安全相关接口")
public class SecurityApi extends BaseApi {

	@Autowired
	private ApiClientService apiClientService;

	@Autowired
	private ApiClientRecordService apiClientRecordService;

	@RequestMapping(value = "findApiClientPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询ApiClient", notes = "分页查询ApiClient")
	public void findApiClientPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<ApiClient> result = new PageObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<ApiClient> apiClientPage = apiClientService.findApiClientPage(pageNo, pageSize, searchParams,
					sortParam, sortType);
			JsonPage<ApiClient> jsonPage = new JsonPage<ApiClient>();
			jsonPage.setList(apiClientPage.getEntities());
			jsonPage.setPageNo(apiClientPage.getPageNo());
			jsonPage.setPageSize(apiClientPage.getPageSize());
			jsonPage.setTotalCount(apiClientPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addApiClient.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增ApiClient", notes = "新增ApiClient")
	public void addApiClient(String email, String description, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<ApiClient> result = new SingleObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			ApiClient apiClient = apiClientService.addApiClient(email, description);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(apiClient);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateApiClient.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新ApiClient", notes = "更新ApiClient")
	public void updateApiClient(Long id, Long yesCounts, String description, Integer status, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<ApiClient> result = new SingleObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			ApiClient apiClient = apiClientService.updateApiClient(id, yesCounts, description, status);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(apiClient);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findAll.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取全部ApiClient", notes = "获取全部ApiClient")
	public void findAll(HttpServletRequest request, HttpServletResponse response) {
		ListObject<ApiClient> result = new ListObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<ApiClient> items = apiClientService.findAll();
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(items);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addApiClientRecord.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增ApiClient记录", notes = "新增ApiClient记录")
	public void addApiClientRecord(Long apiClientId, Long dateTime, Long counts, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<ApiClientRecord> result = new SingleObject<>();
		try {
			// Long _apiClientId = (Long) request.getAttribute("apiClientId");
			ApiClientRecord record = apiClientRecordService.addApiClientRecord(apiClientId, dateTime, counts);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(record);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findApiClientRecordPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询ApiClient记录", notes = "分页查询ApiClient记录")
	public void findApiClientRecordPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, Long apiClientId, Long startTime, Long endTime, HttpServletRequest request,
			HttpServletResponse response) {
		PageObject<ApiClientRecord> result = new PageObject<>();
		try {
			// Long _apiClientId = (Long) request.getAttribute("apiClientId");
			Date startDate = null;
			if (startTime != null) {
				startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startTime), "yyyy-MM-dd");
			}
			Date endDate = null;
			if (endTime != null) {
				endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endTime), "yyyy-MM-dd");
			}
			Page<ApiClientRecord> recordPage = apiClientRecordService.findApiClientRecordPage(apiClientId, pageNo,
					pageSize, searchParams, sortParam, sortType, startDate, endDate);
			JsonPage<ApiClientRecord> jsonPage = new JsonPage<ApiClientRecord>();
			jsonPage.setList(recordPage.getEntities());
			jsonPage.setPageNo(recordPage.getPageNo());
			jsonPage.setPageSize(recordPage.getPageSize());
			jsonPage.setTotalCount(recordPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findApiClientRecordTotalTimes.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询apiClient总共调用次数", notes = "查询apiClient总共调用次数")
	public void findApiClientRecordTotalTimes(Long apiClientId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Long> result = new SingleObject<>();
		try {
			// Long _apiClientId = (Long) request.getAttribute("apiClientId");
			Long totalTimes = apiClientRecordService.findApiClientRecordTotalTimes(apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(totalTimes);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findApiClientRecordMeanTimes.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询apiClient评均调用次数", notes = "查询apiClient评均调用次数")
	public void findApiClientRecordMeanTimes(Long apiClientId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Long> result = new SingleObject<>();
		try {
			// Long _apiClientId = (Long) request.getAttribute("apiClientId");
			Long meanTimes = apiClientRecordService.findApiClientRecordMeanTimes(apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(meanTimes);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
